define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
	const controller = {
		index() {
			Table.api.init({
				extend: {
					index_url: 'business/busrecyclebin/index',
					restore_url: 'business/busrecyclebin/restore',
					destroy_url: 'business/busrecyclebin/destroy',
					table: 'business',
				},
			});

			const table = $('#table');

			table.bootstrapTable({
				// 请求地址
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				// 设置工具栏元素
				toolbar: '.toolbar',
				// 设置主键
				pk: 'id',
				// 排序字段
				sortName: 'id',
				// 默认排序方式
				sortOrder: 'desc',
				showExport: false,
				// 渲染表格数据
				columns: [
					{ checkbox: true },
					{ field: 'id', title: __('Id'), sortable: true },
					{
						field: 'nickname',
						title: __('Bnickname'),
						operate: 'LIKE',
					},
					{
						field: 'mobile',
						title: __('Bmobile'),
						operate: 'LIKE',
					},
					{
						field: 'source.name',
						title: __('SName'),
						operate: 'LIKE',
					},
					{
						field: 'sex_text',
						title: __('BsexText'),
						sortable: false,
						searchable: false,
					},
					{
						field: 'deal',
						title: __('BdealText'),
						searchList: { 0: __('未成交'), 1: __('已成交') },
						formatter: Table.api.formatter.normal,
					},
					{
						field: 'admin.username',
						title: __('Ausername'),
						operate: 'LIKE',
					},
					{
						field: 'visit.createtime',
						title: __('Screatetime'),
						sortable: false,
						searchable: false,
						formatter: Table.api.formatter.datetime,
						operate: 'RANGE',
						addclass: 'datetimerange',
					},
					{
						field: 'visit.content',
						title: __('Vcontent'),
						sortable: false,
						searchable: false,
					},
					{
						field: 'operate',
						title: __('Operate'),
						table: table,
						events: Table.api.events.operate,
						buttons: [
							{
								name: 'restore',
								title: '恢复',
								classname: 'btn btn-xs btn-success btn-ajax',
								extend: "data-toggle='tooltip'",
								icon: 'fa fa-reply',
								confirm: '确认恢复？',
								url: $.fn.bootstrapTable.defaults.extend.restore_url,
								success() {
									// 刷新表格
									table.bootstrapTable('refresh');
								},
								error(data, ret) {
									console.log(data, ret);
									Layer.alert(ret.msg);
									return false;
								},
							},
							{
								name: 'destroy',
								title: '销毁',
								classname: 'btn btn-xs btn-danger btn-ajax',
								extend: "data-toggle='tooltip'",
								icon: 'fa fa-trash',
								confirm: '确认销毁？',
								url: $.fn.bootstrapTable.defaults.extend.destroy_url,
								success(data, ret) {
									Layer.alert(ret.msg);
									// 刷新表格
									table.bootstrapTable('refresh');
								},
								error(data, ret) {
									console.log(data, ret);
									Layer.alert(ret.msg);
									return false;
								},
							},
						],
						formatter: Table.api.formatter.operate,
					},
				],
			});

			// 点击还原回调
			$('.btn-restore').click(() => {
				const ids = Table.api.selectedids(table);

				layer.confirm(
					'确定恢复吗？',
					{
						title: '提示',
						btn: ['确定', '取消'],
					},
					(index) => {
						Backend.api.ajax(
							{
								url: $.fn.bootstrapTable.defaults.extend.restore_url + '?ids=' + ids,
							},
							() => {
								//关闭弹框
								layer.close(index);

								//刷新表格
								table.bootstrapTable('refresh');
							},
							(err, res) => {
								console.log(err, res);
								layer.close(index);
							}
						);
					}
				);
			});

			// 点击还原回调
      $('.btn-destroy').click(() => {
        const ids = Table.api.selectedids(table);

				layer.confirm(
					'确定销毁吗？',
					{
						title: '提示',
						btn: ['确定', '取消'],
					},
					(index) => {
						Backend.api.ajax(
							{
								url: $.fn.bootstrapTable.defaults.extend.destroy_url + '?ids=' + ids,
							},
							() => {
								//关闭弹框
								layer.close(index);

								//刷新表格
								table.bootstrapTable('refresh');
							},
							(err, res) => {
								console.log(err, res);
								layer.close(index);
							}
						);
					}
				);
      });

			Table.api.bindevent(table);
		},
		recovery() {
			controller.api.bindevent();
		},
		apply() {
			controller.api.bindevent();
		},
		del() {
			controller.api.bindevent();
		},
		api: {
			// JS模块化的全局方法
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};

	return controller;
});
